﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FooFun.DataElements.Dtos;
using FooFun.Faq.Dal.Services;
using PhysicalDataProvider;
using FAQFrameworks;
using AccessGrant.GrantFrameworks;
using DTOFrameworks;

namespace FooFun.Faq.Bll
{
    public class RoleSetManager
    {
        RoleSetService roleSetService;
        DEDataProvider dataProvider;


        public RoleSetManager()
        {
            dataProvider = new DEDataProvider();
            roleSetService = new RoleSetService();

            roleSetService.DataProvider = dataProvider;
        }



        //    "UserId", "RoleId", "ForumId" roleSetService.ListDataElement();        
        public IDataElement ListRoleSetByAccountId(Guid accountId)
        {
            dataProvider.Begin();
            IDataElement de = roleSetService.ListDataElement();
            dataProvider.End();

            return new DutiesOfUserDataElement(de, accountId);
        }

        public class DutiesOfUserDataElement : CustomDataElement
        {
            Guid accountId;

            public DutiesOfUserDataElement(IDataElement de, Guid accountId)
                : base(de)
            {
                this.accountId = accountId;
            }

            public override System.Collections.IEnumerator GetEnumerator()
            {
                foreach (IDataElement item in this.InnerDataElement)
                {
                    Guid guid = DataProviderUtil.GetGuid(item["UserId"].Value);

                    if (guid == this.accountId)
                    {
                        IDataElement dutyElement = new DutyDataElement(item);

                        yield return dutyElement;
                    }
                }
            }
        }

        public class DutyDataElement : CustomDataElement
        {
            public DutyDataElement(IDataElement de)
                : base(de)
            {

            }

            UserManager userManager = new UserManager();
            ForumManager forumManager = new ForumManager();

            public override IDataElement this[string attrName]
            {
                get
                {
                    switch (attrName)
                    {
                        case "UserId":
                            return userManager.GetElement(base[attrName]);
                        case "RoleId":
                            IEnumItem item = PermissionQuery.Create().GetAccountById(
                                DataProviderUtil.GetGuid(base[attrName].Value)
                                );

                            return new StringDataElement(item.Name);

                        case "ForumId":
                            return forumManager.GetElement(base[attrName]);
                        default:
                            break;
                    }

                    return base[attrName];
                }
                set
                {
                    base[attrName] = value;
                }
            }
        }
    }
}
